<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <!-- 表格操作 -->
    <div class="operation-container">
      <!-- 条件筛选 -->
      <div class="operation-container">
        <el-radio-group v-model="registrationStatus">
          <el-radio :label="3">全部</el-radio>
          <el-radio :label="0">备选项</el-radio>
          <el-radio :label="1">备选项</el-radio>
          <el-radio :label="2">备选项</el-radio>
        </el-radio-group>
        <!-- 科室 -->
        <el-cascader :options="departmentList" v-model="departmentId" :show-all-levels="false"
                     placeholder="请选择科室" clearable size="small" style="margin-right:1rem" @change="cascaderChange"
        >
        </el-cascader>
        <!-- 职称 -->
        <el-select
            clearable
            v-model="professional"
            placeholder="请选择职称"
            size="small"
            style="margin-right:1rem"
        >
          <el-option
              v-for="item in professionalList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
          />
        </el-select>
        <el-input
            v-model="keywords"
            prefix-icon="el-icon-search"
            size="small"
            placeholder="请输入医生昵称"
            style="width:200px"
            @keyup.enter.native="searchUsers"
        />
        <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            style="margin-left:1rem"
            @click="searchUsers"
        >
          搜索
        </el-button>
      </div>
    </div>
    <!-- 表格展示 -->
    <el-table border :data="doctorList" v-loading="loading">
      <!-- 表格列 -->
      <el-table-column
          prop="avatar"
          label="头像"
          align="center"
          width="140"
      >
        <template>
          <img src="@/assets/images/avatar/doctor_avatar.png" width="60" height="60"/>
        </template>
      </el-table-column>
      <el-table-column
          prop="departmentName"
          label="科室"
          align="center"
          width="140"
      />
      <el-table-column
          prop="name"
          label="姓名"
          align="center"
          width="120"
      />
      <el-table-column
          prop="professional"
          label="职称"
          align="center"
          width="140"
      >
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.professional }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="doctorIntroduction"
          label="简介"
          align="center"
          width="490"
          show-overflow-tooltip
      />
      <el-table-column
          prop="specializedDirection"
          label="擅长方向"
          align="center"
          width="280"
          show-overflow-tooltip
      />
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="130"
          align="center"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right:5px"/>
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <!-- 列操作 -->
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
              type="primary"
              size="mini"
              @click="openModel(scope.row,2)"
              style="margin-right: 0.1rem"
          >
            <i class="el-icon-edit"/>编辑
          </el-button>
          <el-button
              type="primary"
              size="mini"
              @click="deleteDoctor(scope.row.id)"
          >
            <i class="el-icon-delete"/>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        class="pagination-container"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="current"
        :page-size="size"
        :total="count"
        :page-sizes="[10, 20]"
        layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 修改对话框 -->
    <el-dialog :visible.sync="addDoctor" width="30%">
      <div class="dialog-title-container" slot="title" ref="doctorTitle"/>
      <el-form label-width="60px" size="medium" :model="doctorForm">
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="/api/admin/doctor/avatar"
              :show-file-list="false"
              :on-success="updateAvatar"
              :headers="headers"
          >
            <el-avatar :size="80" v-if="doctorForm.avatar" :src="doctorForm.avatar" shape="circle"></el-avatar>
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" required>
          <el-input v-model="doctorForm.name" style="width:220px"/>
        </el-form-item>
        <!-- 科室 -->
        <el-form-item label="科室" required>
          <el-cascader :options="departmentList" v-model="doctorForm.departmentId" :show-all-levels="false"
                       placeholder="请选择科室" clearable size="small" style="margin-right:1rem">
          </el-cascader>
        </el-form-item>
        <el-form-item label="职称" required>
          <el-select
              clearable
              v-model="doctorForm.professional"
              placeholder="请选择职称"
              size="small"
              style="margin-right:1rem"
          >
            <el-option
                v-for="item in professionalList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="简介" required>
          <el-input v-model="doctorForm.doctorIntroduction" style="width:300px" type="textarea" :rows="8"
                    maxlength="1024"
                    show-word-limit/>
        </el-form-item>
        <el-form-item label="擅长方向" label-width="70" required>
          <el-input v-model="doctorForm.specializedDirection" style="width:300px" type="textarea" :rows="8"
                    maxlength="1024"
                    show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addDoctor = false">取 消</el-button>
        <el-button type="primary" @click="editDoctor">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  created() {
    this.listDoctors();
    this.listDepartments();
  },
  data: function () {
    return {
      headers: this.$store.state.headers,
      registrationStatus: 0,
      loading: true,
      addDoctor: false,
      show: true,
      doctorForm: {
        departmentId: null,
        name: "",
        avatar: "",
        professional: "",
        doctorIntroduction: "",
        specializedDirection: ""
      },
      professional: null,
      departmentIdList: [],
      departmentId: null,
      doctorList: [],
      departmentList: [],
      professionalList: [
        {
          id: 1,
          name: "主任医师"
        },
        {
          id: 2,
          name: "副主任医师"
        },
        {
          id: 3,
          name: "主治医师"
        },
        {
          id: 4,
          name: "住院医师"
        }
      ],
      keywords: null,
      current: 1,
      size: 10,
      count: 0
    };
  },
  methods: {
    searchUsers() {
      this.current = 1;
      this.listDoctors();
    },
    sizeChange(size) {
      this.size = size;
      this.listDoctors();
    },
    currentChange(current) {
      this.current = current;
      this.listDoctors();
    },
    updateAvatar(response) {
      if (response.flag) {
        this.$message.success(response.message);
        this.doctorForm.avatar = response.data;
        //this.$store.commit("updateAvatar", response.data);
      } else {
        this.$message.error(response.message);
      }
    },
    openModel(doctor, type) {
      if (doctor) {
        this.show = false;
        //传入的type是1就是新增医生
        switch (type) {
          case 1:
            this.doctorForm = {
              departmentId: null,
              name: "",
              avatar: "",
              professional: "",
              doctorIntroduction: "",
              specializedDirection: ""
            };
            this.$refs.doctorTitle.innerHTML = "新增医生";
            break;
            //传入的type是2就是修改医生
          case 2:
            this.$refs.doctorTitle.innerHTML = "修改医生";
            this.doctorForm = JSON.parse(JSON.stringify(doctor));
            //this.doctorForm.departmentId = this.doctorForm.departmentId[1];
            break;
        }
      } else {
        this.$refs.doctorTitle.innerHTML = "新增医生";
        this.show = true;
        this.doctorForm = {
          departmentId: null,
          name: "",
          avatar: "",
          professional: "",
          doctorIntroduction: "",
          specializedDirection: ""
        };
      }
      this.addDoctor = true;
    },
    deleteDoctor(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete("/api/admin/deleteDoctor/" + id).then(({data}) => {
              if (data.flag) {
                this.$notify.success({
                  title: "成功",
                  message: "删除成功"
                });
                this.listDoctors();
              } else {
                this.$notify.error({
                  title: "失败",
                  message: "删除失败"
                });
              }
            });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    editDoctor() {
      this.axios
          .post("/api/admin/saveOrUpdateDoctor", this.doctorForm)
          .then(({data}) => {
            if (data.flag) {
              this.$notify.success({
                title: "成功",
                message: data.message
              });
              this.listDoctors();
            } else {
              this.$notify.error({
                title: "失败",
                message: data.message
              });
            }
            this.addDoctor = false;
          });
    },
    cascaderChange() {
      this.departmentId = this.departmentIdList[1];
    },
    listDoctors() {
      this.axios
          .get("/api/admin/listDoctor", {
            params: {
              current: this.current,
              size: this.size,
              keywords: this.keywords,
              professional: this.professional,
              departmentId: this.departmentId
            }
          })
          .then(({data}) => {
            this.doctorList = data.data.recordList;
            this.count = data.data.count;
            this.loading = false;
          });
    },
    listDepartments() {
      this.axios
          .get("/api/admin/listDepartment", {})
          .then(({data}) => {
            this.departmentList = data.data;
            console.log(this.departmentList)
          });
    }
    // listRoles() {
    //   this.axios.get("/api/admin/users/role").then(({data}) => {
    //     this.userRoleList = data.data;
    //   });
    // }
  },
};
</script>
